<template>
	<view :style="[$func.bg('bg.jpg')]" class="main_bg login_main">

		<app-header title="登录"></app-header>
		 
		<view class="login_content flex-c">
			<view class="subtitle">登录数字人小程序，获取更多服务。</view>
			<view class="login">
				<view class="nologin" @click="close(1)">暂不登录</view>
				<!-- 	<button class='authBtn' type='primary' open-type='getUserProfile' @click='getUserProfile'  v-if="showMobile">
					授权登录
				</button> -->
				<button class='authBtn' type='primary' open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"
					v-if="showMobile">授权登录</button>
				<button class='authBtn' type='primary' @click="close(2)" v-else>
					授权登录
				</button>

			</view>
		</view>

	</view>
</template>

<script>
	export default {
		components: {

		},
		data() {
			return {
				info: {},
				showMobile: false,
				code: "",
				token:""
			}
		},

		onBackPress(event) {

		},
		onLoad() {
			let _this = this
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					if (loginRes.errMsg == "login:ok") {
						_this.code = loginRes.code
						_this.getInfo()
					} else {
						uni.showToast({ title: "授权失败，稍后重试", icon: 'none' });
					}
				},
				fail: err => {
					console.log(err)
				}
			});
		},
		methods: {
		 
			close(type) {
				if (type == 1) {
					this.$db.save('token', "")
					this.$store.commit('setToken', "")
					this.$store.commit('setIsLogin', false)
				}
				else {
					this.$db.save('token', this.token)
					this.$store.commit('setToken', this.token)
					this.$store.commit('setIsLogin', true)
				}
				uni.navigateBack({
					delta: 1
				});
				// this.$emit('login',{})
			},
			getCode() {
				let _this = this
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						if (loginRes.errMsg == "login:ok") {
							_this.code = loginRes.code

						} else {
							uni.showToast({ title: "授权失败，稍后重试", icon: 'none' });
						}
					},
					fail: err => {
						console.log(err)
					}
				});
			},
			getInfo() {
				this.$http.login("/data/api.Wxapp/session", { code: this.code }).then(res => {
					if (res.code == 1) {
						this.token = res.data.token.token
						this.$store.commit('setUser', res.data)
						if (res.data.mobile) {
							this.showMobile = false
							
						} else {
							this.showMobile = true
						}
						this.getCode()
					}
				}).catch(res => {
					this.getCode()
				})
			},
			getPhoneNumber(e) {
				uni.openPrivacyContract({
				  success: () => {}, // 打开成功
				  fail: () => {}, // 打开失败
				  complete: () => {}
				})
				console.log(e)
				// 参数e是绑定的授权方法自动传入过来的, 为了获取到iv和encryptedData值，让后端解析手机号
				this.$http.login("/data/api.wxapp/decode", {
					code: this.code,
					iv: e.detail.iv,
					encryptedData: e.detail.encryptedData
				}).then(res => {
					if (res.code == 1) {
						uni.showToast({
							title: "登录成功",
							duration: 2000
						});
						this.$db.save('token', res.data.token.token)
						this.$store.commit('setToken', res.data.token.token)
						this.$store.commit('setIsLogin', true)

						uni.navigateBack({
							delta: 1
						});
						this.getCode()
					}
				})

			},
			 


		},
		onPullDownRefresh() {

		},
		onReachBottom() {


		}
	}
</script>

<style scoped lang="scss">
	.login_content {
		height: calc(100vh - 120px);
		flex-direction: column;

	}

	.login_main {


		.title {
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: bold;
			line-height: 40rpx;
			color: #333333;
			display: flex;
			align-items: center;

			padding: 30rpx;

		}

		.subtitle {
			padding: 30rpx 0;
			color: #333333;
			font-size: 30rpx;
			border-bottom: 1px solid #EEEEEE;
			margin: 0 30rpx 64rpx;

		}

		.login {
			display: flex;
			justify-content: center;
			align-items: center;

			font-size: 29rpx;

			font-weight: 400;
			color: #FFFFFF;

			.nologin {
				width: 188rpx;
				height: 90rpx;
				background: #9B751C;
				border-radius: 10rpx;
				font-size: 34rpx;
				font-family: PingFang SC;
				font-weight: 400;
				line-height: 40rpx;
				color: rgba(255, 245, 117, 1);
				display: flex;
				justify-content: center;
				align-items: center;
			}

			button {
				background-color: rgba(0, 0, 0, 0);
				display: flex;
				justify-content: center;
				align-items: center;
				border-color: transparent;
				border-style: none;
				width: 200rpx;
				height: 90rpx;
				background: rgba(255, 245, 117, 1);
				border-radius: 10rpx;
				font-size: 34rpx;
				font-family: PingFang SC;
				font-weight: 400;

				color: #9B751C;
				margin: 0 0 0 130rpx;
			}

			button::after {
				border: none;
			}
		}
	}
</style>